// packagePay/pages/new-select-list/new-select-list.js
import { getProjectList } from '../../../api/app'
import { addFromProject } from '../../api/pay'
import user from '../../../utils/user'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [
            '全部',
            '无打卡',
            '需打卡'
        ],
        current:0,
        pageNum:1,
        isDelete:false,
        hasNextPage:false,
        list: [],
        checkedId:'',
        checkedPeopleNum:''
    },
    onLoad (options) {

    },
    onShow () {
        user.waitLogin().then(()=>{
            this.getProjectList()
        })
    },
    //
    cancel(){
        this.setData({
            isDelete:false
        })
    },
    showDeterminePopup (){
        if(!this.data.checkedId){
            app.toast('请至少选择一个')
            return
        }
        const arr = this.data.list.map((item,index)=>{
            if(item.checked){
                return item.peopleNum
            }
        }).filter(n=>n)
        this.setData({
            isDelete:true,
            checkedPeopleNum:arr[0]
        })
    },
    async onDelete(){
        await addFromProject({
            
        })
        this.cancel()
    },
    switchtab(event) {
        this.setData({
            current: event.currentTarget.dataset.index,
            pageNum: 1,
            list: []
        })
        this.getProjectList()
    },
    // 选择单个
    onChecked (event) {
        console.log(event, 'event')
        const { id } = event.currentTarget.dataset
        this.data.list.forEach((item,index)=>{
            if(item.id == this.data.checkedId){
                this.setData({
                    checkedId:'',
                    ['list['+ index +'].checked']:false
                })
            }
        })
        this.data.list.forEach((item,index)=>{
            if(item.id == id){
                this.setData({
                    checkedId:id,
                    ['list['+ index +'].checked']:true
                })
                console.log(this.data.list, '错误第二层')
            }
        })
    },
    // 到达底部事件
    onScroll () {
        console.log('到达底部啦')
        if(!this.data.hasNextPage){
            return
        }
        this.setData({
            pageNum:this.data.pageNum += 1
        })
        console.log(this.data.pageNum,'pageNum')
        this.getProjectList()
    },
    // 获取数据
    async getProjectList () {
        // 改接口不需要搜索 后期可能添加
        const result = await getProjectList({
            companyId: user.companyId,
            type: this.data.current ? (this.data.current - 1) : null,
            params: {
                userId: user.userId
            },
            pageNum: this.data.pageNum,
            pageSize:10,
            searchName:''
        })
        const arr = this.data.list
        arr.push(...result.list)
        this.setData({
            list:arr,
            hasNextPage:result.hasNextPage
        })
        console.log(this.data.list,'listlist')
        console.log(result, 'result')
    }
})